package com.rxt.thermopro.ui.chart

import android.annotation.SuppressLint
import android.content.Context
import android.widget.TextView
import com.github.mikephil.charting.components.MarkerView
import com.github.mikephil.charting.data.CandleEntry
import com.github.mikephil.charting.data.Entry
import com.github.mikephil.charting.highlight.Highlight
import com.github.mikephil.charting.utils.MPPointF
import com.github.mikephil.charting.utils.Utils
import com.rxt.thermopro.R
import com.rxt.thermopro.app.LocalDataDao
import com.rxt.thermopro.bean.ProbeHistoryBean
import com.rxt.thermopro.bean.TempBean
import java.text.SimpleDateFormat
import java.util.*

class HistoryMarkerView(context: Context, layoutResource: Int,
                        val list: MutableList<ProbeHistoryBean>,
                        val is24:Boolean)
    : MarkerView(context, layoutResource) {

    private var tvContent: TextView = findViewById(R.id.tvContent)
    val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm")
    private val dateFormat2 = SimpleDateFormat("yyyy-MM-dd-HH-mm")
    private val dateFormat1 = SimpleDateFormat("dd-MM-yyyy hh:mm")

    @SuppressLint("SetTextI18n")
    override fun refreshContent(e: Entry?, highlight: Highlight?) {

        if (e is CandleEntry) {
            tvContent.text = "02:00PM  87°F " +
                    Utils.formatNumber(e.high, 0, true) + "%"
        } else {
            val date = Date()
            date.time = list[(e?.x?:0).toInt()].tempTime.toLong()
            val timeStr = if (is24){
                dateFormat.format(date)
            }else{
                dateFormat1.format(date)
            }
            val strTrip = getAMOrPM(date,is24)
            val str = strTrip+timeStr.split(" ")[1]
            tvContent.text = "$str  ${e?.y?.toInt()}°"
        }

        super.refreshContent(e, highlight)
    }

    override fun getOffset(): MPPointF {
        return MPPointF((-(width / 2)).toFloat(), (-height / 2).toFloat())
    }


    /**
     * AM 还是 PM
     */
    private fun getAMOrPM(date: Date, is24: Boolean): String {
        return if (!is24) {
            val str = dateFormat2.format(date)
            val str1 = str.split("-")[3]
            if (str1.toInt() > 12) {
                "PM "
            } else {
                "AM "
            }
        } else {
            ""
        }
    }

}